package com.sibrary.booxoo;

import com.sibrary.utils.Constants;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class BooxooHomePageActivity extends Activity {

	private Spinner mSpinner;
	private EditText mEditText;

	private Button mBtnOk;
	private Button mBtnCancel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.booxoo_homepage_activity);

		initSomeSourceData();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		String[] spinnerValues = getResources().getStringArray(
				R.array.HomepageSpinnerValues);
		for (int i = 0; i < spinnerValues.length; ++i) {
			adapter.add(spinnerValues[i]);
		}

		mSpinner.setAdapter(adapter);

		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// Spinner spinner = (Spinner) parent;
				ArrayAdapter<String> adapter = (ArrayAdapter<String>) mSpinner
						.getAdapter();
				String title = adapter.getItem(position);

				if (position == 0) {
					mEditText.setText(Constants.URL_ABOUT_BLANK);
					mEditText.setEnabled(false);
				} else {
					mEditText.setEnabled(true);
					mEditText.selectAll();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// nothing to do
			}
		});

		mBtnOk.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				String mHomepage = mEditText.getText().toString();
				if (mHomepage != null) {
					Editor editor = Constants.mSharedPreferences.edit();
					editor.putString(Constants.PREFERENCE_KEY_HOMEPAGE,
							mHomepage);
					editor.commit();
				}
				finish();
			}
		});

		mBtnCancel.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		setSpinnerShowData();
	}

	/**
	 * 初始化一些信息
	 */
	private void initSomeSourceData() {
		mSpinner = (Spinner) findViewById(R.id.HomepageActivity_homepageSpinner);
		mEditText = (EditText) findViewById(R.id.HomepageActivity_homepageEditText);

		mBtnOk = (Button) findViewById(R.id.HomepageActivity_homepageBtnOK);
		mBtnCancel = (Button) findViewById(R.id.HomepageActivity_homepageBtnCancel);
	}

	/**
	 * 设置spinner的当前显示项
	 */
	private void setSpinnerShowData() {
		String mHomepage = Constants.mSharedPreferences.getString(
				Constants.PREFERENCE_KEY_HOMEPAGE, Constants.URL_ABOUT_BLANK);
		if (mHomepage == Constants.URL_ABOUT_BLANK) {
			mSpinner.setSelection(0);
		} else {
			mEditText.setText(mHomepage);
			mSpinner.setSelection(1);
		}
	}
}
